package com.education.teacher.controller;

import com.education.model.StudentModel;
import com.education.teacher.redis.RedisPubSubManager;
import com.education.util.EmailMsgModel;
import com.education.util.SendMailUtil;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.web.bind.annotation.*;
import com.alibaba.dubbo.config.annotation.Reference;
import com.education.model.CourseInform;
import com.education.model.ResultDo;
import com.education.service.ICourseServiceApi;
import com.github.pagehelper.PageInfo;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

import javax.servlet.http.HttpSession;
import java.text.ParseException;
import java.util.List;

/**
 * 课程通知实例
 * @author 赵睿慷
 *
 */
@Api(value = "/api/course", description = "课程通知")
@RestController
@RequestMapping("/api/course")
public class CourseInformController {
    
    /**
     * 注入用户的业务层
     */
    @Reference
    private ICourseServiceApi icsc;


    @Autowired
    @Qualifier("emailPubManager")
    private RedisPubSubManager redisPubSubManager;
    
    /**
     * 引入log4j日志记录
     */
    private  Logger logger = Logger.getLogger(CourseInformController.class);

    /**
     *
     * @param dateRange
     * @param pageSize
     * @param pageNum
     * @param session
     * @return
     */
    @ApiOperation(notes = "likeCourse", httpMethod = "GET", value = "查看课程通知信息，精确查询模糊查询")
    @RequestMapping(value="/likeCourse",method=RequestMethod.GET)
    public ResultDo<PageInfo<CourseInform>> getCourseInform (@RequestParam(value = "dateRange[]",required = false) String[] dateRange, Integer pageSize, Integer pageNum, HttpSession session) throws ParseException {


        int tid=Integer.parseInt((String) session.getAttribute("teacherId"));
        /**
         * 前台传值可能为undefined和null
         */
        if ("undefined".equals(dateRange[0])||"null".equals(dateRange[1])){
            dateRange=null;
        }
        PageInfo<CourseInform> pageInfo=icsc.searchInformByDataRange(dateRange,tid,pageNum,pageSize);
        ResultDo<PageInfo<CourseInform>> res = new ResultDo<>();
        res.setResData(pageInfo);
        return res;
    }
    
    /**
     * 删除信息
     * @param informId 课程管理通知
     * @return int.
     */
    @ApiOperation(notes = "remove", httpMethod = "GET", value = "删除课程通知信息")
    @RequestMapping(value="/remove",method=RequestMethod.GET)
    public ResultDo delete(int informId){
        int del = icsc.deleteCourseById(informId);
        ResultDo<String> rs = new ResultDo();
        if(del == 0){
            rs.setResCode(103);
        }
        return rs;
    }
    
    /**
     * 修改
     * 2017/8/8 完全修改 by 雷紫辉
     * @param courseInform 传入实体类对象
     * @return int.
     */
    @ApiOperation(notes = "editCourse", httpMethod = "POST", value = "修改课程通知信息")
    @RequestMapping(value="/editCourse",method=RequestMethod.POST)
    public ResultDo update(@RequestBody CourseInform courseInform){

        int update = icsc.updateCourseById(courseInform);//修改的肯定时自己发布的，不用设置教师id
        ResultDo rs = new ResultDo();
        if(update == 0){
            rs.setResCode(102);
        }

        List<StudentModel> list=icsc.getStudentByTeacherId(courseInform.getTeacherId());
        String courseName=icsc.selectCourseById(courseInform.getTeacherId());
        for (StudentModel s:list){
            EmailMsgModel emailMsgModel=new EmailMsgModel();
            emailMsgModel.setSubject(s.getStudentName()+":你"+courseInform.getInformDateStr()+"的课程【"+courseName+"】的通知有变动");
            emailMsgModel.setMsg(courseInform.getInformInformTitle());
            emailMsgModel.setAddTo(s.getStudentEmail());
            redisPubSubManager.pubMessage(emailMsgModel);//将邮件消息塞入队列中
        }
        return rs;
    }
    
    /**
     * 增加
     * @param courseInform 传入实体类对象
     * @return 参数.
     */
    @ApiOperation(notes = "add", httpMethod = "POST", value = "增加课程通知信息")
    @RequestMapping(value="/add",method=RequestMethod.POST)
    public ResultDo add(@RequestBody CourseInform courseInform,HttpSession session){


        int tid=Integer.parseInt((String) session.getAttribute("teacherId"));

        courseInform.setTeacherId(tid);
        int add = icsc.addCourseInfo(courseInform);
        ResultDo<Void> rs = new ResultDo<>();
        if (add == 0) {
            rs.setResCode(101);
        }
        List<StudentModel> list=icsc.getStudentByTeacherId(tid);
        String courseName=icsc.selectCourseById(tid);
        for (StudentModel s:list){
            EmailMsgModel emailMsgModel=new EmailMsgModel();
            emailMsgModel.setSubject(s.getStudentName()+":你"+courseInform.getInformDateStr()+"的课程【"+courseName+"】有新的通知");
            emailMsgModel.setMsg(courseInform.getInformInformTitle());
            emailMsgModel.setAddTo(s.getStudentEmail());
            redisPubSubManager.pubMessage(emailMsgModel);//将邮件消息塞入队列中
        }

        return rs;
    }
}
